package com.cn.jinl.utils;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import com.cn.jinl.domain.Sterilize;
import freemarker.cache.FileTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class WordUtil {

    private static boolean license = false;

    private static void loadLicense(String path) {
        if (license) {
            return;
        }
        try {
            // license.xml放在src/main/resources文件夹下
            InputStream is = new FileInputStream(path);
            License aposeLic = new License();
            aposeLic.setLicense(is);
            license = true;
        } catch (Exception e) {
            license = false;
            System.out.println("License验证失败...");
            e.printStackTrace();
        }
    }

    public static String doc2pdf(String wordPath, String pdfPath, String licensePath) {
        loadLicense(licensePath);
        if (!license) {
            System.out.println("License验证不通过...");
            return "";
        }
        try {
            long old = System.currentTimeMillis();
            //新建一个pdf文档
            File file = new File(pdfPath);
            FileOutputStream os = new FileOutputStream(file);
            //Address是将要被转化的word文档
            Document doc = new Document(wordPath);
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            os.close();
            //转化用时
            System.out.println("Word 转 Pdf 共耗时：" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            System.out.println("Word 转 Pdf 失败...");
            e.printStackTrace();
        }
        return pdfPath;
    }

//    public static String generatePdfPath(String ftlPath, String downPath, Object object)throws Exception{
//        String random =  CommonUtil.generateRandomNum("ftl-");
//        Configuration configuration = new Configuration(new Version("2.3.28"));
//        configuration.setDefaultEncoding("utf-8");
//        TemplateLoader templateLoader = new FileTemplateLoader(new File(ftlPath));
//        configuration.setTemplateLoader(templateLoader);
//        String wordNamePath = downPath + random + ".doc";
//        File outFile = new File(wordNamePath);
//        Template template = configuration.getTemplate("art_up.ftl", "utf-8");
//        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
//        template.process(null, out);
//        out.close();
//        String pdfName = random + ".pdf";
//        String pdfPath = downPath + pdfName;
//        WordUtil.doc2pdf(wordNamePath, pdfPath, ftlPath + "license.xml");
//        return pdfName;
//    }

    public static String generateSterilizePdfPath(String ftlPath, String downPath, String ftlName, Object object) throws Exception {
        String random = CommonUtil.generateRandomNum("ftl-");
        Configuration configuration = new Configuration(new Version("2.3.28"));
        configuration.setDefaultEncoding("utf-8");
        TemplateLoader templateLoader = new FileTemplateLoader(new File(ftlPath));
        configuration.setTemplateLoader(templateLoader);
        String wordNamePath = downPath + random + ".doc";
        File outFile = new File(wordNamePath);
        Template template = configuration.getTemplate(ftlName, "utf-8");
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
        template.process(object, out);
        out.close();
        String pdfName = random + ".pdf";
        String pdfPath = downPath + pdfName;
        WordUtil.doc2pdf(wordNamePath, pdfPath, ftlPath + "license.xml");
        return pdfName;
    }

    //投诉答复
    public static String generateTouSuPdfPath(String fileName, String ftlPath, String downPath, Map<String, Object> result) throws Exception {
        String random = CommonUtil.generateRandomNum("ftl-");
        Configuration configuration = new Configuration(new Version("2.3.28"));
        configuration.setDefaultEncoding("utf-8");
        TemplateLoader templateLoader = new FileTemplateLoader(new File(ftlPath));
        configuration.setTemplateLoader(templateLoader);
        String wordNamePath = downPath + random + ".doc";
        File outFile = new File(wordNamePath);
        Template template = configuration.getTemplate("toushu1.ftl", "utf-8");
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
        template.process(result, out);
        out.close();
        String pdfPath = downPath + fileName;
        WordUtil.doc2pdf(wordNamePath, pdfPath, ftlPath + "license.xml");
        return fileName;
    }

}
